<template>
  <div class="w-card content-box">
    <div class="title-box">
      <span class="title">售卡文件详情 - {{ fileName }}</span>
      <div style="margin-left: auto;">
        <el-button type="warning" plain @click="exportHandle(1)">导出</el-button>
      </div>
    </div>

    <div class="my-search-box">
      <div>
        <span>用户手机号：</span>
        <el-input v-model="searchForm.filePhone" clearable placeholder="请输入用户手机号" />
      </div>
      <div>
        <span>代理商：</span>
        <el-select v-model="searchForm.shopId" filterable clearable placeholder="请选择">
          <el-option v-for="item in shopList" :key="item.shopId" :label="item.shopName" :value="item.shopId" />
        </el-select>
      </div>
      <div>
        <span>校验结果：</span>
        <el-select v-model="searchForm.fileStatus" filterable clearable placeholder="请选择">
          <el-option label="未计算" value="0" />
          <el-option label="有效" value="1" />
          <el-option label="无效" value="2" />
        </el-select>
      </div>
      <div class="f-ml-20">
        <el-button @click="searchHandle" type="primary">查询</el-button>
        <el-button @click="resetHandle">重置</el-button>
      </div>
    </div>

    <div class="table-box">
      <el-table :max-height="tableHeight" :header-cell-style="{background:'#d6dee5',color:'#000000',fontSize:'14px'}"
        :cell-style="{fontSize:'14px',color:'#7b7b7d'}" :data="tableData" stripe @selection-change="handleSelectionChange">
        <el-table-column type="index" width="55" label="序号" align="center" />
        <!-- <el-table-column type="selection" width="55" /> -->
        <el-table-column prop="filePhone" label="用户手机号" align="center">
          <template v-slot="scope">
            {{ scope.row.filePhone || '--' }}
          </template>
        </el-table-column>
        <el-table-column prop="shopName" label="店铺名称" align="center">
          <template v-slot="scope">
            {{ scope.row.shopName }}
          </template>
        </el-table-column>
        <el-table-column prop="fileFee" label="金额(元)" align="center">
          <template v-slot="scope">
            {{ scope.row.fileFee }}
          </template>
        </el-table-column>
        <el-table-column prop="income" label="返佣金额(元)" align="center">
          <template v-slot="scope">
            {{ scope.row.income }}
          </template>
        </el-table-column>
        <el-table-column prop="fileStatus" label="校验结果" align="center">
          <template v-slot="scope">
            {{ fileStatus[scope.row.fileStatus] }}
          </template>
        </el-table-column>
        <el-table-column prop="errorMsg" label="备注" align="center">
          <template v-slot="scope">
            {{ scope.row.errorMsg }}
          </template>
        </el-table-column>
      </el-table>
      <div class="my-table-footer">
        <!-- <div>
              <el-button :disabled="chooseList.length == 0" @click="exportHandle(2)">批量导出</el-button>
            </div> -->
        <el-pagination style="margin-left:auto;" v-model:currentPage="page" v-model:page-size="size"
          :page-sizes="[10, 20, 50, 100]" small :background="true" layout="total, sizes, prev, pager, next, jumper" :total="total"
          @size-change="handleSizeChange" @current-change="handleCurrentChange" />
      </div>
    </div>
  </div>
</template>
    
<script>
import { onMounted, reactive, toRefs, watch, ref, nextTick } from 'vue'
import { getWindowWidthAndHeight, warningDeal, getDateTime, BlobDownLoad } from '@/utils/utils'
import { useRoute, useRouter } from 'vue-router'
import api from './api'

export default {
  setup() {
    const route = useRoute()
    const router = useRouter()

    const state = reactive({
      page: 1,
      size: 10,
      total: 0,
      tableHeight: 0,
      tableData: [],
      chooseList: [],

      shopList: [],

      fileName: route.query.fileName,
      searchForm: {
        "filePhone": "",
        "fileStatus": "",
        "recordId": route.query.recordId,
        "shopId": "",
        "sysOrderId": ""
      },

      fileStatus: {
        '0': '未计算',
        '1': '有效',
        '2': '无效',
      },
    })

    function getTableData() {
      const obj = {
        page: state.page,
        size: state.size,
        ...state.searchForm,
        orderBy: 'id desc',
      }
      api.settleImportDetail(obj).then(res => {
        if (res.code == '200') {
          state.tableData = res.data.list
          state.total = res.data.total
        } else {
          warningDeal(res.msg)
        }
      }).catch(err => {
        console.log(err)
      })
    }

    function handleCurrentChange(e) {
      state.page = e
      getTableData()
    }

    function handleSizeChange(e) {
      state.size = e
      getTableData()
    }

    function handleSelectionChange(e) {
      state.chooseList = e
    }

    function searchHandle() {
      state.page = 1
      getTableData()
    }

    function resetHandle() {
      state.searchForm = {
        "filePhone": "",
        "fileStatus": "",
        "recordId": route.query.recordId,
        "shopId": "",
        "sysOrderId": ""
      }
      searchHandle()
    }

    function exportHandle() {
      let obj = {
        page: state.page,
        size: state.size,
        ...state.searchForm,
        orderBy: 'id desc',
      }
      api.settleImportDetailDown({ ...obj, responseType: "blob" }).then(res => {
        const _fileName = decodeURIComponent(res.headers['content-disposition'].split(';')[1].split('=')[1])
        BlobDownLoad(res, _fileName)
      }).catch(err => {
        console.log('err', err)
      })
    }

    const getShopList = () => {
      api.shopList().then(res => {
        state.shopList = res.data.list
      })
    }

    onMounted(() => {
      let winHeight = getWindowWidthAndHeight()
      state.tableHeight = winHeight.windowHeight - 330
      getTableData()
      getShopList()
    })

    return {
      ...toRefs(state),
      exportHandle,
      handleCurrentChange,
      handleSizeChange,
      searchHandle,
      resetHandle,
      getDateTime,
      handleSelectionChange,
    }
  }
}
    </script>
    
<style lang="scss" scoped>
.content-box {
  height: 100%;
  overflow: hidden;
  overflow-y: auto;
  box-sizing: border-box;
  padding: 20px;
  .title-box {
    display: flex;
    align-items: center;
    > .title {
      font-size: 16px;
      font-weight: bold;
    }
  }
  .my-search-box {
    box-sizing: border-box;
    padding: 20px 20px 20px 0;
    padding-bottom: 0;
    font-size: 14px;
    display: flex;
    align-items: center;
    gap: 10px;

    > div {
      width: 350px;
      display: flex;
      align-items: center;

      > span {
        flex-shrink: 0;
        width: 90px;
        text-align: right;
      }
    }

    .datebox {
      width: 450px;
    }
  }
  .table-box {
    margin-top: 10px;
    .my-table-footer {
      display: flex;
      align-items: center;
      margin-top: 10px;
      flex-wrap: wrap;
      gap: 10px;
    }
  }
}

.oStatus {
  display: flex;
  align-items: center;
  gap: 5px;
  &::before {
    content: "";
    display: block;
    width: 8px;
    height: 8px;
    border-radius: 50%;
    flex-shrink: 0;
  }
}
.oStatus1 {
  &::before {
    background-color: #e6a23c;
  }
}
.oStatus2 {
  &::before {
    background-color: #b7b9bd;
  }
}
.oStatus3 {
  &::before {
    background-color: #f04134;
  }
}
.oStatus4 {
  &::before {
    background-color: #00a854;
  }
}
.oStatus5 {
  &::before {
    background-color: #e6a23c;
  }
}
</style>